<?php
namespace app\common\library\xilujob\resume;


use app\common\library\xilujob\ResumeLibrary;
use app\common\model\xilujob\Resume;
use app\common\model\xilujob\ResumeSummary;
use think\Model;

class Summary extends BaseModule {

    protected $name = ResumeLibrary::MODULE_SUMMARY;

    public function onCreateResume(Resume $resume) {
        $model = $this->getModel();
        $ret = $model->save(['user_id'=>$resume->user_id, 'resume_id'=>$resume->id]);
        if($ret===false) {
            throw new ModuleException($model->getError());
        }
    }

    public function add($data) {
        throw new ModuleException("Module '{$this->name}' does not support add method");
    }

    public function del($id) {
        throw new ModuleException("Module '{$this->name}' does not support del method");
    }

    /**
     * 获取所有字段的key
     * @return array
     */
    protected function keys() {
        return ['content'];
    }

    /**
     * 获取验证规则
     * @return array
     */
    protected function getRules() {
        return [
            'content' => 'require|length:1,300',
        ];
    }

    /**
     * 获得Model实例
     * @return Model
     */
    protected function getModel() {
        return new ResumeSummary();
    }

    /**
     * @return array score: 当前分数， max: 满分
     */
    public function getCompleteScore() {
        $max = 10;
        $count = ResumeSummary::where('resume_id',$this->resume->id)->where('content','<>', '')->count();
        $score = $count>0 ? 10 : 0;
        return ['score'=>$score, 'max'=>$max];
    }

}